package com.lutao.util;


import com.baidu.aip.imageclassify.AipImageClassify;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Master.Lu
 * @version V1.0
 * @name: BaiduPictureRecognitionUtil
 * @package com.lutao.util
 * @description TODO:
 * @date 2018-01-02 12:00
 */
public class BaiduPictureRecognitionUtil {
    public static final String APP_ID = "10225463";
    public static final String API_KEY = "gkAqQEgN3jlk18PKCUfDAnPH";
    public static final String SECRET_KEY = "DnKahpBpR4DtzN7GwDqdSUdZ9akE4Qrx";
    public static Map<String,Object> pictureRecognition(byte[] file, int type){
        Map<String, Object> map=new HashMap<String,Object>();
        switch (type){
            case 0:
                map = animalDetectRecognition(file);
                break;
            case 1:
                map = plantDetectRecognition(file);
                break;
            case 2:
                map = dishDetectRecognition(file);
                break;
            case 3:
                map = carDetectRecognition(file);
                break;
            case 4:
                map = logoSearchRecognition(file);
                break;
            default:
                map.put("code","fail");
                map.put("mes","未完待续");
                break;
        }
        return map;
    }
    public static Map<String,Object> pictureRecognition(String file, int type){
        Map<String, Object> map=new HashMap<String,Object>();
        switch (type){
            case 0:
                map = animalDetectRecognition(file);
                break;
            case 1:
                map = plantDetectRecognition(file);
                break;
            case 2:
                map = dishDetectRecognition(file);
                break;
            case 3:
                map = carDetectRecognition(file);
                break;
            case 4:
                map = logoSearchRecognition(file);
                break;
            default:
                map.put("code","fail");
                map.put("mes","未完待续");
                break;
        }
        return map;
    }
    private static Map<String,Object> animalDetectRecognition(byte[] file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.animalDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","动物识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","动物识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> plantDetectRecognition(byte[] file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.plantDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","植物识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> dishDetectRecognition(byte[] file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.dishDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","食物识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> carDetectRecognition(byte[] file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.carDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> logoSearchRecognition(byte[] file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("custom_lib", "false");
            org.json.JSONObject response = client.logoSearch(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }

    private static Map<String,Object> animalDetectRecognition(String file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.animalDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
           map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> plantDetectRecognition(String file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.plantDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> dishDetectRecognition(String file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.dishDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> carDetectRecognition(String file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "3");
            org.json.JSONObject response = client.carDetect(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }
    private static Map<String,Object> logoSearchRecognition(String file){
        Map<String, Object> paramMap = null, map=new HashMap<String,Object>();
        try {
            // 初始化一个AipImageClassifyClient
            AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("custom_lib", "false");
            org.json.JSONObject response = client.logoSearch(file,options);
            JSONObject object = JSONObject.fromObject(response.toString());
            JSONArray a  = object.getJSONArray("result");
            map.put("code","success");
            map.put("mes","识别成功");
            map.put("data",a);
        }catch (Exception e){
            map.put("code","fail");
            map.put("mes","识别异常");
            return map;
        }
        return map;
    }

//    public static void main(String[] arg) {
//        Map<String, Object>  map=new HashMap<String,Object>();
//        String file = "C:\\animal.jpg";
//        map = pictureRecognition(file,1);
//        System.out.println(map);
//    }
}
